// 资讯列表
var totalPage,
    pageIndex = 1;

// 默认加载
getGalleryData(pageIndex);

// 刷新&加载

var listWrap = document.getElementById('data_list'),
    loadText = document.querySelector('.loadmore-text'),
    startPos = 0,
    transitionHeight = 0;

// 获取当前滚动条的位置 
function getScrollTop() {
    var scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop;
    } else if (document.body) {
        scrollTop = document.body.scrollTop;
    }
    return scrollTop;
}

// 获取当前可视范围的高度 
function getClientHeight() {
    var clientHeight = 0;
    if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
    } else {
        clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
    }
    return clientHeight;
}

// 获取文档完整的高度 
function getScrollHeight() {
    return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}

// 节流函数
var throttle = function (method, context) {
    clearTimeout(method.tId);
    method.tId = setTimeout(function () {
        method.call(context);
    }, 300);
}

function fetchData() {
    // alert(totalPage)
    // alert(pageIndex)
    if (pageIndex < totalPage) {
        pageIndex++;
        console.log(totalPage, pageIndex)

        getGalleryData(pageIndex);
    } else {
        loadText.innerText = '没有更多了';
        setTimeout(function () {
            $(".loadmore-text").stop().slideUp();
        }, 1000);
    }
}

window.onscroll = function () {
    // alert("文档高度：" + getScrollHeight())
    // alert("可视区域：" + getClientHeight())
    if (getScrollTop() + getClientHeight() == getScrollHeight()) {
        // alert("a：" + ( getClientHeight() + getScrollTop()) + "  b:" + getScrollHeight())
        loadText.innerText = '加载中，请稍候';
        throttle(fetchData);
    }
};


// 请求数据
function getGalleryData(PageIndex) {
    // if (PageIndex == 1) {
    //     // 请求数据
    //     $.ajax({
    //         type: "get",
    //         url: requestUrl + "/getNewsList.aspx",
    //         data: "&pageindex=" + pageIndex + "&pagesize=5",
    //         dataType: "json",
    //         success: function (json) {
    //             alert(json.totalPage);
    //             totalPage = json.totalPage;
    //             $(".article-list").empty();
    //             $.each(json.items, function (index, item) {
    //                 var html = loadItem(item);
    //                 $(".article-list").append(html);
    //             });
    //             $(".loadmore-text").show();
    //         }
    //     });
    // } else {
    // 请求数据
    requestAjax("get", requestUrl + "/getNewsList.aspx", "pageindex=" + pageIndex + "&pagesize=5", false, (json) => {
        totalPage = json.totalPage;
        if(pageIndex == 1 &&  json.items.length ==0){
            $(".list-empty").show();
        }else{
            $(".list-empty").hide();
            $.each(json.items, function (index, item) {            
                var html = loadItem(item);
                $(".article-list").append(html);
            });
            $(".loadmore-text").show();
        }
        
    });
    // }
}

// 数据渲染
function loadItem(item) {
    var html = '<div class="list-item"><div class="list-head"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-write"></use></svg></span>' + item.publishDate + '</div><div class="list-content">';
    // 置顶文章、视频大图展示
    if (item.newsList[0].newsType == "图文") {
        var tempHtml = $("#item_article_big").html();
        $.each(item.newsList[0], function (key, val) {
            tempHtml = tempHtml.replace(new RegExp("{{" + key + "}}", "gim"), val);
        });
        html += tempHtml;
    } else {
        var tempHtml = $("#item_video_big").html();
        $.each(item.newsList[0], function (key, val) {
            tempHtml = tempHtml.replace(new RegExp("{{" + key + "}}", "gim"), val);
        });
        html += tempHtml;
    }
    // 删除第一项
    item.newsList.shift();
    // 加载剩余列表项
    $.each(item.newsList, function (index, item) {
        var tempHtml;
        if (item.newsType == "图文") {
            tempHtml = $("#item_article").html();
        } else {
            tempHtml = $("#item_video").html();
        }
        $.each(item, function (key, val) {
            tempHtml = tempHtml.replace(new RegExp("{{" + key + "}}", "gim"), val);
        });
        html += tempHtml;
    })
    html += '</div></div>';
    return html;
}